package com.hxzy.common.config.shiro;


import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.List;

/**
 * @author 自定义验证
 */
@Log4j2
public class CustomRealm extends AuthorizingRealm {


//    @Lazy
//    @Autowired
//    private UserService userService;
//   @Lazy
//    @Autowired
//    private RoleService roleService;
//
//    @Lazy
//    @Autowired
//    private ModuleService moduleService;


    /**
     * 为该用户分权角色和权限
     * admin=密码不要,adminstrator
     * hr=api:role:search,api:role:insert
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

      //  User dbUser= (User) principals.getPrimaryPrincipal();


        SimpleAuthorizationInfo   simpleAuthorizationInfo=new SimpleAuthorizationInfo();
//        //查询该用户拥有的角色权限
//        List<Role> dbRoleList=this.roleService.ownerRoles(dbUser.getUuid());
//        dbRoleList.stream().forEach(p-> {
//            simpleAuthorizationInfo.addRole(p.getRoleRemark());
//        });
//
//        logger.debug(simpleAuthorizationInfo.getRoles().toString());
//
//        //查询该用户拥有的角色的菜单权限
//        List<Module> dbModuleList=this.moduleService.ownerModules(dbUser.getUuid());
//        dbModuleList.stream().forEach(p->{
//            simpleAuthorizationInfo.addStringPermission(p.getModulePerms());
//        });
//        logger.debug(simpleAuthorizationInfo.getStringPermissions().toString());

        return simpleAuthorizationInfo;
    }

    /**
     * 登录，验证用户和密码是否正确
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        AuthenticationInfo  authenticationInfo=null;
//
//        //连接数据库
//        if(token instanceof UsernamePasswordToken){
//            String loginName=((UsernamePasswordToken) token).getUsername();
//            if(StringUtils.isBlank(loginName)){
//                return null;
//            }
//
//            String password= new String(((UsernamePasswordToken) token).getPassword());
//            UserLoginDTO  loginDTO=new UserLoginDTO();
//            loginDTO.setLoginName(loginName);
//            loginDTO.setLoginPassword(password);
//            //调用数据库
//            User dbUser= this.userService.login(loginDTO);
//            if(dbUser==null){
//                throw new UnknownAccountException("用户名或密码错误");
//            }else{
//                //判断密码对不
//                if(!BCryptUtil.checkpw(password, dbUser.getLoginPassword())){
//                    throw new IncorrectCredentialsException("用户名或密码错误");
//                }
//                //返回对象
//                authenticationInfo=new SimpleAuthenticationInfo(dbUser, password, super.getName());
//            }
//        }

        return authenticationInfo;
    }


    /**
     * 重写方法,清除当前用户的的 授权缓存
     * @param principals
     */
    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    /**
     * 重写方法，清除当前用户的 认证缓存
     * @param principals
     */
    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    /**
     * 自定义方法：清除所有 授权缓存
     */
    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    /**
     * 自定义方法：清除所有 认证缓存
     */
    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    /**
     * 自定义方法：清除所有的  认证缓存  和 授权缓存
     */
    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }
}
